class Position {
	constructor(x, y) {
		this.x = x;
		this.y = y;
	}
	getX() {
		return this.x;
	}
	getY() {
		return this.y;
	}
}
class Line {
	constructor(x1, y1) {
		this._constructorSvg(x1, y1);
		this.wayPoint = [];
		this.wayPoint.push(new Position(x1, y1));
		this.first = null;
		this.middle = null;
		this.end = null;
	}

	_constructorSvg(x1, y1) {
		let line = document.createElementNS("http://www.w3.org/2000/svg", "polyline");
		// line.setAttribute("x1", x1);
		// line.setAttribute("y1", y1);
		// line.setAttribute("x2", x1);
		// line.setAttribute("y2", y1);
		line.setAttribute("points", x1 + "," + y1 + " " + x1 + "," + y1);
		line.setAttribute("style", "fill:white;stroke:#000;stroke-width:2");
		line.setAttribute("class", "node");
		this.node = line;
		return this.node;
	}

	passBy(x, y) {
		this.wayPoint.push(new Position(x, y));
	}
	_calcuPoints() {
		if (this.wayPoint.length > 0) {
			this.first = this.wayPoint[0];
			this.middle = this.wayPoint[parseInt(this.wayPoint.length / 2)];
			this.end = this.wayPoint[this.wayPoint.length - 1];
			/* 处理成可行的折线 */
			this.middle.y = this.first.y;
			let realEndX = this.end.x;
			this.end.x = this.middle.x;
			this.finishP = new Position(realEndX, this.end.y);
		}
	}
	format() {
		this._calcuPoints();
		let points = this.first.getX() + "," + this.first.getY() + " " + this.middle.getX() + "," + this
			.middle
			.getY() + " " + this.end.getX() + "," + this.end.getY() + " " + this.finishP.getX() + "," + this.finishP.getY();
		console.log(points)
		this.node.setAttribute("points", points);
	}
	finish(x2, y2) {
		this.passBy(x2, y2);
		// this._calcuPoints();
		this.node.setAttribute("points", this.wayPoint[0].getX() + "," + this.wayPoint[0].getY() + " " + this.wayPoint[this.wayPoint
				.length - 1].getX() +
			"," + this.wayPoint[this.wayPoint.length - 1].getY());
	}

	getNode() {
		return this.node;
	}
}
